#coding=utf8
import itchat
import requests
from itchat.content import *



###########################################
# 图灵机器人
###########################################

def get_response(msg):
    apiUrl = 'http://www.tuling123.com/openapi/api'
    data = {
        'key':'71f28bf79c820df10d39b4074345ef8c',
        'info': msg,
        'userid':'wechat-robot',
    }
    try:
        r = requests.post(apiUrl, data=data).json()
        return r.get('text')
    except:
        return



# 回复指定群组信息
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
    group = itchat.search_chatrooms(name='悄悄地说话，不装')
    groupName = group[0]['UserName']
    print(msg['ActualNickName']+'说：' + msg['Text'])
    text = get_response(msg['Text'])
    print('机器人自动回复：'+text)
    itchat.send(text,toUserName=groupName)

itchat.auto_login(hotReload=True)
itchat.run()